package com.hzy.service;

import com.hzy.dao.BookingMapper;
import com.hzy.pojo.Booking;
import com.hzy.utils.MybatisUtil;
import com.hzy.utils.Page;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * @Package: com.hzy.service
 * @ClassName: BookingService
 * @Author: 862982795
 * @CreateTime: 2021/07/01 17:21
 * @Description:
 */
public class BookingService extends MybatisUtil {
    private final Logger logger=Logger.getLogger(BookingService.class);
    //获取SqlSession实例
    SqlSession sqlSession = createSqlSession();
    List<Booking> list;
    Integer i;

    // 根据条件搜索到的总记录数
    public Integer getCount(Page page) {
        try {
            i = sqlSession.getMapper(BookingMapper.class).getCount(page);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    // 搜索、分页、查询
    public List<Booking> getListByUserNameOrCardIdAndLimit(Page page) {
        //获取SqlSession实例
        SqlSession sqlSession = createSqlSession();
        try {
            list = sqlSession.getMapper(BookingMapper.class).getListByUserNameOrCardIdAndLimit(page);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    // 根据身份证号 cardId 查看详细信息
    public Integer queryBookingByCardId(String cardId) {
        try {
            i = sqlSession.getMapper(BookingMapper.class).queryBookingByCardId(cardId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }

        return i;
    }

    // 添加 booking 对象
    public Integer addBooking(Booking booking) {
        try {
            i = sqlSession.getMapper(BookingMapper.class).addBooking(booking);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    // 根据主键 id 删除单条记录
    public Integer deleteBookingById(Integer id) {
        try {
            i = sqlSession.getMapper(BookingMapper.class).deleteBookingById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    // 根据 id 修改记录
    public Integer updateBooking(Booking booking) {
        try {
            i = sqlSession.getMapper(BookingMapper.class).updateBooking(booking);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    // 根据 id 查看 记录
    public Booking QueryBookingById(Integer id){
        Booking booking=new Booking();
        try {
            booking = sqlSession.getMapper(BookingMapper.class).QueryBookingById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return booking;
    }

    // 修改接种状态
    public Integer updateStatus(Integer id){
        try {
            i = sqlSession.getMapper(BookingMapper.class).updateStatus(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

}
